import { Orders } from './api';
import { $ResponseCode } from './const/env';
export const usePay = (OrderInfo: any) => {
    let OrderStateNum = 0;
    const onBridgeReady = (resolve: any, reject: any) => {
        WeixinJSBridge.invoke('getBrandWCPayRequest', OrderInfo.pay, (res: any) => {
            if (res.err_msg == "get_brand_wcpay_request:ok") {
                OrderStateNum = 1;
                getOrderState(resolve, reject);
            } else {
                reject();
            }
        });
    }
    const getOrderState = (resolve: any, reject: any) => {
        Orders.getOrderState(OrderInfo.order.trade).then((res: any) => {
            if (res.code === $ResponseCode.SUCCESS) {
                resolve();
            } else if (res.code === $ResponseCode.PAY_NOTPAY) {
                OrderStateNum++;
                if (OrderStateNum > 10) {
                    reject({ msg: '支付失败' });
                    return;
                }
                setTimeout(() => {
                    getOrderState(resolve, reject);
                }, 1000)
            }
        }).catch((err) => {
            reject(err);
        })
    }
    return new Promise((resolve, reject) => {
        switch (OrderInfo.pay_type) {
            case 'wxpay':
                // #ifdef H5
                if (typeof WeixinJSBridge == "undefined") {
                    if (document.addEventListener) {
                        document.addEventListener('WeixinJSBridgeReady', () => {
                            onBridgeReady(resolve, reject)
                        }, false);
                    } else if (document.attachEvent) {
                        document.attachEvent('WeixinJSBridgeReady', () => {
                            onBridgeReady(resolve, reject)
                        });
                        document.attachEvent('onWeixinJSBridgeReady', () => {
                            onBridgeReady(resolve, reject)
                        });
                    }
                } else {
                    onBridgeReady(resolve, reject);
                }
                // #endif
                // #ifdef MP-WEIXIN
                uni.requestPayment({
                    provider: 'wxpay',
                    timeStamp: OrderInfo.pay.timeStamp,
                    nonceStr: OrderInfo.pay.nonceStr,
                    package: OrderInfo.pay.package,
                    signType: OrderInfo.pay.signType,
                    paySign: OrderInfo.pay.paySign,
                    orderInfo: OrderInfo.orderInfo,
                    success: (res) => {
                        OrderStateNum = 1;
                        getOrderState(resolve, reject);
                    },
                    fail: () => {
                        reject({ msg: '支付失败' });
                    }
                });
                // #endif
                // #ifdef APP
                uni.requestPayment({
                    provider: "wxpay",
                    orderInfo: OrderInfo.pay,
                    success: (res) => {
                        OrderStateNum = 1;
                        getOrderState(resolve, reject);
                    },
                    fail: () => {
                        reject({ msg: '支付失败' });
                    }
                })
                // #endif       
                break;
            case 'alipay':
            case 'epay':
                // #ifdef H5
                window.location.href = OrderInfo.pay_url;
                // #endif
                break;
        }
    })
}